﻿using LeRenard.ScriptWars.Model.Entities;
using LeRenard.ScriptWars.Model.Scripting;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace LeRenard.ScriptWars.Model.GridSystem
{
    /// <summary>
    /// Places a set of one or more entities on a grid.
    /// </summary>
    public class RandomEntityGridInitializer : DefaultGridInitializer<IEntity>
    {
        /// <summary>
        /// Holds the pseudo random generator.
        /// </summary>
        private static Random _random = new Random();

        /// <summary>
        /// Gets the scripts to be populated on the grid.
        /// </summary>
        public IEnumerable<IScript> Scripts { get { return new ReadOnlyCollection<IScript>(_scripts.ToList()); } }
        private readonly IEnumerable<IScript> _scripts;

        /// <summary>
        /// Creates a new <see cref="RandomGridInitializer"/> object.
        /// </summary>
        /// <param name="randomValueGenerator"></param>
        public RandomEntityGridInitializer(IEnumerable<IScript> scripts)
            : base()
        {
            ThrowIf.Null(() => scripts);

            _scripts = scripts;
        }

        /// <summary>
        /// Generates a value for a <see cref="GridCell"/>.
        /// </summary>        
        public override IEntity GenerateValue()
        {
            if (20 > _random.Next(0, 100)) {
                // Generate an entity
                var initialDirection = (Direction)_random.Next(0, 3);
                var scriptNumber = _random.Next(Scripts.Count());
                var entity = new Entity(Scripts.ElementAt(scriptNumber), initialDirection);                
                return entity;
            } else {
                // Leave it blank
                return null;
            }
        }
    }
}
